package bb.lanxing.calc.data;

import android.util.Log;

import com.baidu.mapapi.model.LatLng;
import com.github.mikephil.charting.utils.Utils;

import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.model.database.Trackpoint;
import bb.lanxing.model.database.Workout;

public class SourcePoint {
    final private static String TAG = "SourcePoint";
    private Double altitude;
    private CadencePoint cadencePoint;
    private double distance;
    private double distanceEach;
    private long duration;
    private long durationEach;
    private float elevation;
    private double elevationGain;
    private double elevationLoss;
    private GpsPoint gpsPoint;
    private float grade;
    private HeartratePoint heartratePoint;
    private PowerratePoint powerratePoint;
    private BiketrainerPoint biketrainerPoint;
    private IndoorbikePoint indoorbikePoint;
    private CrosstrainerPoint crosstrainerPoint;
    private RowerPoint rowerPoint;
    private boolean isOldPoint;
    private float power;
    private float indoorbikepower;
    private String powerExt;
    private double speed;
    private int steps;
    private long timestamp;

    public SourcePoint() {
        this.timestamp = System.currentTimeMillis();
    }

    public SourcePoint copy(SourcePoint sourcePoint) {
        if (sourcePoint == null) {
            sourcePoint = new SourcePoint();
        }
        sourcePoint.speed = this.speed;
        sourcePoint.altitude = this.altitude;
        sourcePoint.timestamp = this.timestamp;
        sourcePoint.isOldPoint = this.isOldPoint;
        sourcePoint.elevation = this.elevation;
        sourcePoint.grade = this.grade;
        sourcePoint.power = this.power;
        sourcePoint.indoorbikepower = this.indoorbikepower;
        sourcePoint.powerExt = this.powerExt;
        sourcePoint.distanceEach = this.distanceEach;
        sourcePoint.durationEach = this.durationEach;
        sourcePoint.distance = this.distance;
        sourcePoint.duration = this.duration;
        sourcePoint.elevationGain = this.elevationGain;
        sourcePoint.elevationLoss = this.elevationLoss;
        sourcePoint.steps = this.steps;
        GpsPoint gpsPoint = this.gpsPoint;
        if (gpsPoint != null) {
            sourcePoint.gpsPoint = gpsPoint.copy(sourcePoint.gpsPoint);
        } else {
            sourcePoint.gpsPoint = null;
        }
        CadencePoint cadencePoint = this.cadencePoint;
        if (cadencePoint != null) {
            sourcePoint.cadencePoint = cadencePoint.copy(sourcePoint.cadencePoint);
        } else {
            sourcePoint.cadencePoint = null;
        }
        HeartratePoint heartratePoint = this.heartratePoint;
        if (heartratePoint != null) {
            sourcePoint.heartratePoint = heartratePoint.copy(sourcePoint.heartratePoint);
        } else {
            sourcePoint.heartratePoint = null;
        }
        PowerratePoint powerratePoint = this.powerratePoint;
        if (powerratePoint != null) {
            sourcePoint.powerratePoint = powerratePoint.copy(sourcePoint.powerratePoint);
        } else {
            sourcePoint.powerratePoint = null;
        }

        BiketrainerPoint biketrainerPoint = this.biketrainerPoint;
        if (biketrainerPoint != null) {
            sourcePoint.biketrainerPoint = biketrainerPoint.copy(sourcePoint.biketrainerPoint);
        } else {
            sourcePoint.biketrainerPoint = null;
        }

        IndoorbikePoint indoorbikePoint = this.indoorbikePoint;
        if (indoorbikePoint != null) {
            sourcePoint.indoorbikePoint = indoorbikePoint.copy(sourcePoint.indoorbikePoint);
        } else {
            sourcePoint.indoorbikePoint = null;
        }

        CrosstrainerPoint crosstrainerPoint = this.crosstrainerPoint;
        if (crosstrainerPoint != null) {
            sourcePoint.crosstrainerPoint = crosstrainerPoint.copy(sourcePoint.crosstrainerPoint);
        } else {
            sourcePoint.crosstrainerPoint = null;
        }
        return sourcePoint;
    }

    public SourcePoint(Trackpoint trackpoint, Workout workout) {
        this.timestamp = trackpoint.getTime();
        this.speed = trackpoint.getSpeed();
        this.altitude = trackpoint.getAltitude();
        this.elevation = (float) trackpoint.getElevation();
        this.grade = (float) trackpoint.getGrade();
        this.power = (float) trackpoint.getPower();
        this.powerExt = trackpoint.getPowerExt();
        double latitude = trackpoint.getLatitude();
        double d = Utils.DOUBLE_EPSILON;
        if (latitude > Utils.DOUBLE_EPSILON && trackpoint.getLongitude() > Utils.DOUBLE_EPSILON) {
            GpsPoint gpsPoint = new GpsPoint();
            this.gpsPoint = gpsPoint;
            gpsPoint.setSpeed((float) trackpoint.getSpeed());
            this.gpsPoint.setLatitude(trackpoint.getLatitude());
            this.gpsPoint.setLongitude(trackpoint.getLongitude());
            this.gpsPoint.setAltitude(trackpoint.getAltitude());
            this.gpsPoint.setSpeed((float) trackpoint.getSpeedGps());
            this.gpsPoint.setBearing((float) trackpoint.getBearing());
            this.gpsPoint.setTimestamp(trackpoint.getTime());
            this.gpsPoint.setSource(workout.getLocSource());
        }
        if (trackpoint.getSumCadence() > 0 || trackpoint.getSumWheel() > 0) {
            CadencePoint cadencePoint = new CadencePoint();
            this.cadencePoint = cadencePoint;
            cadencePoint.setSumCadence(trackpoint.getSumCadence());
            this.cadencePoint.setSumWheel(trackpoint.getSumWheel());
            this.cadencePoint.setPressure((int) trackpoint.getPressure());
            this.cadencePoint.setTemperature((int) trackpoint.getTemperature());
            this.cadencePoint.setRealPressure((float) trackpoint.getPressure());
            this.cadencePoint.setRealTemperature((float) trackpoint.getTemperature());
            this.cadencePoint.setWheelRpm(trackpoint.getWheelRpm());
            this.cadencePoint.setCadence(trackpoint.getCadence());
            this.cadencePoint.setSpeed(trackpoint.getSpeedCa() == Utils.DOUBLE_EPSILON ? trackpoint.getSpeed() : d);
            this.cadencePoint.setAltitude((float) trackpoint.getAltitudeCa());
            this.cadencePoint.setSource(workout.getCadenceSource());
        }
        if (trackpoint.getHeartrate() > 0) {
            HeartratePoint heartratePoint = new HeartratePoint();
            this.heartratePoint = heartratePoint;
            heartratePoint.setHeartrate(trackpoint.getHeartrate());
            this.heartratePoint.setSource(workout.getHeartSource());
        }
        if (trackpoint.getPower() > 0) {
            PowerratePoint powerratePoint = new PowerratePoint();
            this.powerratePoint = powerratePoint;
            powerratePoint.setPowerrate((int)trackpoint.getPower());
            this.powerratePoint.setSource(workout.getPowerSource());
        }

        if (trackpoint.getPower() > 0) {
            BiketrainerPoint biketrainerPoint = new BiketrainerPoint();
            this.biketrainerPoint = biketrainerPoint;
            biketrainerPoint.setBiketrainerPower((int)trackpoint.getPower());
            this.biketrainerPoint.setSource(workout.getPowerSource());
        }

        if (trackpoint.getPower() > 0) {
            IndoorbikePoint indoorbikePoint = new IndoorbikePoint();
            this.indoorbikePoint = indoorbikePoint;
            indoorbikePoint.setIndoorbikePower((int)trackpoint.getPower());
            this.indoorbikePoint.setSource(workout.getPowerSource());
        }

        if (trackpoint.getPower() > 0) {
            CrosstrainerPoint crosstrainerPoint = new CrosstrainerPoint();
            this.crosstrainerPoint = crosstrainerPoint;
            crosstrainerPoint.setCrosstrainerPower((int)trackpoint.getPower());
            this.crosstrainerPoint.setSource(workout.getPowerSource());
        }

        if (trackpoint.getPower() > 0) {
            RowerPoint rowerPoint = new RowerPoint();
            this.rowerPoint = rowerPoint;
            rowerPoint.setRowerPower((int)trackpoint.getPower());
            this.rowerPoint.setSource(workout.getPowerSource());
        }

    }

    public Trackpoint toTrackpoint() {
        Trackpoint trackpoint = new Trackpoint();
        trackpoint.setTime(this.timestamp);
        trackpoint.setSpeed(this.speed);
        Double d = this.altitude;
        trackpoint.setAltitude(d == null ? Utils.DOUBLE_EPSILON : d);
        trackpoint.setElevation(this.elevation);
        trackpoint.setGrade(this.grade);
        trackpoint.setPower(this.power);
        trackpoint.setPowerExt(this.powerExt);
        trackpoint.setDistanceEach(this.distanceEach);
        trackpoint.setDurationEach(this.durationEach);
        trackpoint.setDistance(this.distance);
        trackpoint.setDuration(this.duration);
        trackpoint.setElevationGain(this.elevationGain);
        trackpoint.setElevationLoss(this.elevationLoss);
        GpsPoint gpsPoint = this.gpsPoint;
        if (gpsPoint != null) {
            trackpoint.setLatitude(gpsPoint.getLatitude());
            trackpoint.setLongitude(this.gpsPoint.getLongitude());
            trackpoint.setAltitudeGps(this.gpsPoint.getAltitude());
            trackpoint.setAccuracy(this.gpsPoint.getAccuracy());
            trackpoint.setSpeedGps(this.gpsPoint.getSpeed());
            trackpoint.setBearing(this.gpsPoint.getBearing());
            trackpoint.setTime(this.gpsPoint.getTimestamp());
        }
        CadencePoint cadencePoint = this.cadencePoint;
        if (cadencePoint != null) {
            trackpoint.setSumWheel(cadencePoint.getSumWheel());
            trackpoint.setSumCadence(this.cadencePoint.getSumCadence());
            trackpoint.setPressure(this.cadencePoint.getRealPressure());
            trackpoint.setTemperature(this.cadencePoint.getRealTemperature());
            trackpoint.setWheelRpm(this.cadencePoint.getWheelRpm());
            trackpoint.setCadence(this.cadencePoint.getCadence());
            trackpoint.setSpeedCa(this.cadencePoint.getSpeed());
            trackpoint.setAltitudeCa(this.cadencePoint.getAltitude());
        }
        HeartratePoint heartratePoint = this.heartratePoint;
        if (heartratePoint != null) {
            trackpoint.setHeartrate(heartratePoint.getHeartrate());
        }
        PowerratePoint powerratePoint = this.powerratePoint;
        if (powerratePoint != null) {
            trackpoint.setPower(powerratePoint.getPowerrate());
        }

        BiketrainerPoint biketrainerPoint = this.biketrainerPoint;
        if (biketrainerPoint != null) {
            trackpoint.setPower(biketrainerPoint.getBiketrainerPower());
            trackpoint.setCadence(biketrainerPoint.getBiketrainerCadence());
        }

        IndoorbikePoint indoorbikePoint = this.indoorbikePoint;
        if (indoorbikePoint != null) {
            trackpoint.setPower(indoorbikePoint.getIndoorbikePower());
            trackpoint.setCadence(indoorbikePoint.getIndoorbikeCadence());
            trackpoint.setSpeed(indoorbikePoint.getIndoorbikeSpeed());
            trackpoint.setHeartrate(indoorbikePoint.getIndoorbikeHeartRate());
        }

        CrosstrainerPoint crosstrainerPoint = this.crosstrainerPoint;
        if (crosstrainerPoint != null) {
            trackpoint.setPower(crosstrainerPoint.getCrosstrainerPower());
            trackpoint.setSpeed(crosstrainerPoint.getCrosstrainerSpeed());
            trackpoint.setCadence(crosstrainerPoint.getCrosstrainerStepRate());
        }

        RowerPoint rowerPoint = this.rowerPoint;
        if (rowerPoint != null) {
            trackpoint.setPower(rowerPoint.getRowerPower());
            trackpoint.setCadence(rowerPoint.getRowerStrokeRate());
        }
        return trackpoint;
    }

    public boolean hasGps() {
        return this.gpsPoint != null;
    }

    public boolean hasCadence() {
        return this.cadencePoint != null;
    }

    public boolean hasPressure() {
        CadencePoint cadencePoint = this.cadencePoint;
        return cadencePoint != null && cadencePoint.getPressure() > 0;
    }

    public boolean hasCadenceData() {
        CadencePoint cadencePoint = this.cadencePoint;
        return cadencePoint != null && cadencePoint.getSumCadence() > 0;
    }

    public boolean hasWheelData() {
        CadencePoint cadencePoint = this.cadencePoint;
        return cadencePoint != null && cadencePoint.getSumWheel() > 0;
    }

    public boolean hasHeartrate() {
        return this.heartratePoint != null;
    }

    public boolean hasPowerrate() {
        return this.powerratePoint != null;
    }

    public boolean hasBiketrainer() {
        return this.biketrainerPoint != null;
    }

    public boolean hasIndoorbike() {
        return this.indoorbikePoint != null;
    }

    public boolean hasCrosstrainer() {
        return this.crosstrainerPoint != null;
    }

    public boolean hasRower() {
        return this.rowerPoint != null;
    }

    public GpsPoint getGpsPoint() {
        return this.gpsPoint;
    }

    public void setGpsPoint(GpsPoint gpsPoint) {
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_TRAINING) {
            return;
        }
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_INDOOR_BIKE) {
            return;
        }
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_BIKE_TRAINER) {
            return;
        }
        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_ROWER) {
            return;
        }

        if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_CROSS_TRAINER) {
            return;
        }
        this.gpsPoint = gpsPoint;
        if (gpsPoint != null) {
            this.speed = gpsPoint.getSpeed();
            this.altitude = gpsPoint.getAltitude();
            this.timestamp = gpsPoint.getTimestamp();
        }
    }

    public CadencePoint getCadencePoint() {
        return this.cadencePoint;
    }

    public void setCadencePoint(CadencePoint cadencePoint) {
        this.cadencePoint = cadencePoint;
        if (cadencePoint != null) {
            if (RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING) == Workout.TYPE_TRAINING) {
                this.speed = cadencePoint.getSpeed();
            }
            if (hasPressure()) {
                this.altitude = (double) cadencePoint.getAltitude();
            }
        }
    }

    public HeartratePoint getHeartratePoint() {
        return this.heartratePoint;
    }
    public PowerratePoint getPowerratePoint() {
        return this.powerratePoint;
    }
    public BiketrainerPoint getBiketrainerPoint() {
        return this.biketrainerPoint;
    }

    public IndoorbikePoint getIndoorbikePoint() {
        return this.indoorbikePoint;
    }

    public CrosstrainerPoint getCrosstrainerPoint() {
        return this.crosstrainerPoint;
    }

    public RowerPoint getRowerPoint() {
        return this.rowerPoint;
    }
    public void setHeartratePoint(HeartratePoint heartratePoint) {
        this.heartratePoint = heartratePoint;
    }
    public void setPowerratePoint(PowerratePoint powerratePoint) {
        this.powerratePoint = powerratePoint;
    }
    public void setBiketrainerPoint(BiketrainerPoint biketrainerPoint) {
        this.biketrainerPoint = biketrainerPoint;
    }
    public void setIndoorbikePoint(IndoorbikePoint indoorbikePoint) {
        this.indoorbikePoint = indoorbikePoint;
    }

    public void setCrosstrainerPoint(CrosstrainerPoint crosstrainerPoint) {
        this.crosstrainerPoint = crosstrainerPoint;
    }

    public void setRowerPoint(RowerPoint rowerPoint) {
        this.rowerPoint = rowerPoint;
    }
    public LatLng getLatLng() {
        GpsPoint gpsPoint = this.gpsPoint;
        if (gpsPoint == null) {
            return null;
        }
        return gpsPoint.getLatLng();
    }

    public double getSpeed() {
        return this.speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public double getAltitude() {
        Double d = this.altitude;
        return d == null ? Utils.DOUBLE_EPSILON : d;
    }

    public Double getAltitude(Double d) {
        Double d2 = this.altitude;
        return d2 == null ? d : d2;
    }

    public void setAltitude(Double altitude) {
        this.altitude = altitude;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public void setIsOldPoint(boolean isOldPoint) {
        this.isOldPoint = isOldPoint;
    }

    public boolean isOldPoint() {
        return this.isOldPoint;
    }

    public float getElevation() {
        return this.elevation;
    }

    public void setElevation(float elevation) {
        this.elevation = elevation;
    }

    public float getGrade() {
        return this.grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }

    public float getPower() {
        //Log.d(TAG, "getPower this.power=" + this.power);
        return this.power;
    }


    public void setPower(float power) {
        this.power = power;
        //Log.d(TAG, "setPower this.power=" + this.power);
    }

    public float getIndoorbikePower() {
        Log.d(TAG, "getIndoorbikePower this.indoorbikepower=" + this.indoorbikepower);
        return this.indoorbikepower;
    }


    public void setIndoorbikePower(float indoorbikepower) {
        this.indoorbikepower = indoorbikepower;
        Log.d(TAG, "setIndoorbikePower this.indoorbikepower=" + this.indoorbikepower);
    }

    public String getPowerExt() {
        return this.powerExt;
    }

    public void setPowerExt(String powerExt) {
        this.powerExt = powerExt;
    }

    public double getDistanceEach() {
        return this.distanceEach;
    }

    public void setDistanceEach(double distanceEach) {
        this.distanceEach = distanceEach;
    }

    public long getDurationEach() {
        return this.durationEach;
    }

    public void setDurationEach(long durationEach) {
        this.durationEach = durationEach;
    }

    public double getDistance() {
        return this.distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public long getDuration() {
        return this.duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public double getElevationGain() {
        return this.elevationGain;
    }

    public void setElevationGain(double elevationGain) {
        this.elevationGain = elevationGain;
    }

    public double getElevationLoss() {
        return this.elevationLoss;
    }

    public void setElevationLoss(double elevationLoss) {
        this.elevationLoss = elevationLoss;
    }

    public int getSteps() {
        return this.steps;
    }

    public void setSteps(int steps) {
        this.steps = steps;
    }
}
